package com.metis.document.parse.dialog.pipeline;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.metis.document.parse.dialog.models.dto.PDFSizeDTO;
import com.metis.document.parse.dialog.models.dto.ParagraphTextElementDTO;
import com.metis.document.parse.dialog.models.dto.ParseResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.regex.Pattern;

@Slf4j
@Component
public class ProgramFileFilter {

    private final static Pattern pattern = Pattern.compile("[√×÷±≈≠≤≥∞∽≌∝∵∴∑∏∪∩∈∉∅∆∇∂∫∮∟∠∡∢∣∥∦∧∨∬∭∯∰∱∲∳∶∷∸∹∺∻∼∾∿≀≁≂≃≄≅≆≇≉≊≋≍≎≏≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≡≢≣≦≧≨≩≪≫≬≭≮≯≰≱≲≳≴≵≶≷≸≹≺≻≼≽≾≿⊀⊁⊂⊃⊄⊅⊆⊇⊈⊉⊊⊋⊌⊍⊎⊏⊐⊑⊒⊓⊔⊕⊖⊗⊘⊙⊚⊛⊜⊝⊞⊟⊠⊡⊢⊣⊤⊥⊦⊧⊨⊩⊪⊫⊬⊭⊮⊯⊰⊱⊲⊳⊴⊵⊶⊷⊸⊹⊺⊻⊼⊽⊾⊿⋀⋁⋂⋃⋄⋅⋆⋇⋈⋉⋊⋋⋌⋍⋎⋏⋐⋑⋒⋓⋔⋕⋖⋗⋘⋙⋚⋛⋜⋝⋞⋟⋠⋡⋢⋣⋤⋥⋦⋧⋨⋩⋪⋫⋬⋭⋮⋯⋰]");

    private final static int DEFAULT_LENGTH_OF_TOTAL_JSON_RESULT = 50;

    public void detect(@NotNull ParseResultDTO parseResultDTO, @NotNull Set<String> programTag) {
        //检测横版页面
        for (PDFSizeDTO pageSizeInfo : parseResultDTO.getPageSizeInfos()) {
            if (pageSizeInfo.getWidth() > pageSizeInfo.getHeight()) {
                log.info("横版页面");
                programTag.add("横版页面");
            }
        }
        //检测公式乱码
        if (parseResultDTO.getEquation() != null && !parseResultDTO.getEquation().isEmpty() ) {
            log.info("可能存在公式乱码");
            programTag.add("公式乱码");
        }
        for (ParagraphTextElementDTO paragraph : parseResultDTO.getParagraphs()) {
            if (pattern.matcher(paragraph.getText()).find()) {
                log.info("可能存在公式乱码");
                programTag.add("公式乱码");
            }
        }
    }

    public String totalJsonSize(String json){
        String emptyMessage = "文件解析成JSON后内容基本为空";
        if (json.length() <= DEFAULT_LENGTH_OF_TOTAL_JSON_RESULT){
            return emptyMessage;
        }

        try{
            JSONArray content = JSONObject.parseArray(json);
            //JSONArray content = obj.getJSONArray("content");
            if(content == null || content.isEmpty()){
                return emptyMessage;
            }

            StringBuilder sb = new StringBuilder();
            for (Object c : content) {
                if (null != c){
                    String ot = ((JSONObject)c).getString("origin_title");
                    String st = ((JSONObject)c).getString("section_title");
                    String sc = ((JSONObject)c).getString("section_content");
                    if(StringUtils.isNotEmpty(ot)){
                        sb.append(ot);
                    }
                    if(StringUtils.isNotEmpty(st)){
                        sb.append(st);
                    }
                    if(StringUtils.isNotEmpty(sc)){
                        sb.append(sc);
                    }
                }
                if(sb.toString().length() > DEFAULT_LENGTH_OF_TOTAL_JSON_RESULT){
                    return null;
                }
            }

            return emptyMessage;
        }catch (Exception e){
            log.error(e.getMessage());
        }

        return null;
    }
}
